package com.amkai.aes.webservice.binary.objecthandler.inventory;

public class StringHQLGenerator extends PropertyHQLGenerator {
	private boolean caseSensitive;
	private boolean exactMatch;

	public StringHQLGenerator(String instanceName, String propertyName, ResolverServiceManager resolverMgr, boolean caseSensitive, boolean exactMatch) {
		super(instanceName, propertyName, resolverMgr);
		this.caseSensitive = caseSensitive;
		this.exactMatch = exactMatch;
	}

	@Override
	public String getHQLFragment() {
		StringBuilder formatStr = new StringBuilder();
		formatStr.append(caseSensitive ? "%s.%s" : "upper(%s.%s)");
		formatStr.append(exactMatch ? "=?" : " like ?");
		return String.format(formatStr.toString(), instanceName, propertyName);
	}

	@Override
	public Object getValueFromText(String searchText) {
		String result = caseSensitive ? searchText : searchText.toUpperCase();
		return (exactMatch ? result : result + "%");
	}

	@Override
	public String getHQLPropertyName() {
		if (instanceName == null) {
			return propertyName;
		} else {
			return instanceName + "." + propertyName;
		}
	}

}
